package hos.kotlin

import java.text.SimpleDateFormat
import java.util.*


/**
 * <p>Title: TimeExt </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2022/7/1 17:06
 * @version : 1.0
 */

/**
 * Return the current time in milliseconds.
 *
 * @return the current time in milliseconds
 */


@Suppress("SimpleDateFormat")
fun String.format(): SimpleDateFormat {
    return SimpleDateFormat(this)
}

fun String.parse(date: String): Date {
    return format().parse(date)
}

fun String?.time(value: String = ""): String {
    return this.timeNull(value)!!
}

fun String?.timeNull(value: String? = null): String? {
    var time = this.toNullString() ?: return value
    if (!time.contains("T") && !time.contains("+") && !time.contains(".")) {
        return time;
    }
    if (time.contains("T")) {
        time = time.replace("T", " ")
    }
    if (time.contains(".")) {
        time = time.substring(0, time.lastIndexOf("."))
    }
    if (time.contains("+")) {
        time = time.substring(0, time.lastIndexOf("+"))
    }
    return time
}

fun String?.millis(format: String = "yyyy-MM-dd HH:mm:ss", value: Long = -1L): Long {
    val time: String = this.time(value.toString())
    return try {
        format.parse(time).time
    } catch (e: java.lang.Exception) {
        value
    }
}

fun String?.calendar(
    format: String = "yyyy-MM-dd HH:mm:ss",
    value: Calendar = GregorianCalendar()
): Calendar {
    val time: Long = this.millis(format)
    return if (time == -1L) {
        return value
    } else time.calendar(value)
}

fun <K, V> Map<K, V>?.time(key: K, value: String = ""): String {
    return this.toNullString(key).time(value)
}

fun <K, V> Map<K, V>?.timeNull(key: K, value: String? = null): String? {
    return this.toNullString(key).timeNull(value)
}

fun <K, V> Map<K, V>?.calendar(
    key: K,
    format: String = "yyyy-MM-dd HH:mm:ss",
    value: Calendar = GregorianCalendar()
): Calendar {
    val time: Long = this.toNullString(key).millis(format)
    return if (time == -1L) {
        return value
    } else time.calendar(value)
}

fun Long?.date(format: String = "yyyy-MM-dd HH:mm:ss", value: String = ""): String {
    if (this == null || this == -1L) {
        return value
    }
    return format.format().format(Date(this))
}

fun Long?.dateStart(value: String = ""): String {
    return date("yyyy-MM-dd", value) + " 00:00:00"
}

fun Long?.dateEnd(value: String = ""): String {
    return date("yyyy-MM-dd", value) + " 23:59:59"
}
fun Long?.calendar(value: Calendar = GregorianCalendar()): Calendar {
    if (this == null) {
        return value
    }
    val calendar: Calendar = GregorianCalendar()
    calendar.timeInMillis = this
    return calendar
}

fun Calendar?.millis(value: Long = -1L): Long {
    return this?.timeInMillis ?: value
}

fun Calendar?.date(format: String = "yyyy-MM-dd HH:mm:ss", value: String = ""): String {
    val timeLong: Long = millis()
    return if (timeLong == -1L) {
        value
    } else timeLong.date(format)
}

fun Calendar?.dateStart(value: String = ""): String {
    return date("yyyy-MM-dd", value) + " 00:00:00"
}

fun Calendar?.dateEnd(value: String = ""): String {
    return date("yyyy-MM-dd", value) + " 23:59:59"
}

fun String?.convertTime(
    from: String = "yyyy-MM-dd HH:mm:ss", to: String = "yyyy-MM-dd",
    value: String = ""
): String {
    val time: Long = this.millis(from)
    return if (time == -1L) {
        value
    } else time.date(to)
}
